//
//  BuKeInformationVC.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/26.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "BuKeInformationVC.h"
#import "BuKeInformationDetailVC.h"
#import "CJClassSelectedView.h"
#import "CJYearMonthSelectedView.h"
#import "SYBuKeInfoTableViewCell.h"
#import "SYBuKeInviteViewController.h"

@interface BuKeInformationVC ()<BuKeInformationVCNavViewDelegate>{
    NSInteger pageIndex;
}
@property (nonatomic, strong) BuKeInformationDetailVC *buKeXiangQingVC;

@property (nonatomic, strong) SYNoLabel *noLab;
@property (nonatomic, copy) NSString *upordown;//上线下线

@property (nonatomic, copy) NSString *keyWord;//搜索关键字
@property (nonatomic, strong) NSMutableArray *dataListArr;

@end

@implementation BuKeInformationVC
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _upordown = @"1";
    _banjiid = @"";
    _dateTime = @"";
    _keyWord = @"";
    self->pageIndex = 1;
    [self.tableList registerCellWithNIbNames:@[@"SYBuKeInfoTableViewCell"]];
    
}
- (void)initSubView{
    self.mainw = MAINW;
    self.view.backgroundColor = MAINBACKCOLOR;
    [self.view addSubview:self.navView];
    self.dataListArr = [NSMutableArray new];
    [_navView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self.view addSubview:self.topView];
    [_topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(self->_navView.mas_bottom);
        make.height.equalTo(@63);
    }];
    
    [self.view addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0).offset(0);
        make.top.equalTo(self->_navView.mas_bottom);
        make.bottom.equalTo(@0);
    }];
    
    [self addNotice];
    _noLab = [[SYNoLabel alloc] initWithNameStr:@"" contView:self.view];
    [_noLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.navView.mas_bottom).offset(0);
        make.right.left.bottom.equalTo(self.view);
    }];
    __weak typeof(self) weakSelf = self;

//    _topView.block = ^(NSInteger index) {
//        weakSelf.upordown = SJStringWithFormat(index);
//        [weakSelf requestDataListArr];
//    };
    
    _topView.block = ^(NSString * _Nonnull upOrDown, NSString * _Nonnull searchStr) {
        weakSelf.upordown = upOrDown;
        weakSelf.keyWord = searchStr;

//    _topView.block = ^(NSInteger index) {
//        self->pageIndex = 0;
//        [weakSelf.dataListArr removeAllObjects];
//        [weakSelf.tableList reloadData];
//        weakSelf.upordown = SJStringWithFormat(index);

        [weakSelf requestDataListArr];
    };

    
    

    
    [_tableList headerRefreshBlock:^{
        self->pageIndex = 1;
        [self->_dataListArr removeAllObjects];
        [self requestDataListArr];
    }];
    [_tableList fooderRefreshBlock:^{
        self->pageIndex = self->_dataListArr.count/30 >=1?self->_dataListArr.count/30 + 1:2;
        [self requestDataListArr];
    }];

}

- (void)requestDataListArr {
    [MBProgressHUD mj_showMessage:nil toView:self.view];
    if (pageIndex == 1) {
        [self.dataListArr removeAllObjects];
    }
    
    __weak typeof(self) weakSelf = self;
    [SYRequestAPI fd_requestLessonTutorLessonListIsOnLine:_upordown classId:_banjiid date:_dateTime keyWord:_keyWord pageIndex:pageIndex pageSize:30 success:^(NSMutableArray *listArr) {
     //   NSLog(@"得到补课信息列表 %@",listArr);
        [MBProgressHUD mj_hideHUDForView:weakSelf.view];
        [weakSelf.dataListArr addObjectsFromArray:listArr];
//        weakSelf.upordown = @"";
        if (weakSelf.dataListArr.count == 0) {

            weakSelf.noLab.hidden = NO;
            
        }else{
//            self->pageIndex ++;
             weakSelf.noLab.hidden = YES;
            
            weakSelf.tableList.hidden = NO;
            
        }
        [weakSelf.tableList reloadData];
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:weakSelf.view];
        weakSelf.noLab.hidden = NO;
        weakSelf.tableList.hidden = YES;
        [self.tableList.mj_header endRefreshing];
        [self.tableList.mj_footer endRefreshing];
    }];
}


-(void)addNotice{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeView:) name:@"ChangeCellSpaNotice" object:nil];
}
- (BuKeInformationVCNavView *)navView{
    if (!_navView) {
        BuKeInformationVCNavView *topView = [[BuKeInformationVCNavView alloc]initWithFrame:CGRectMake(0, 0, self.mainw, 44)];
        topView.mainDelegate = self;
        topView.titleLabe.text = @"补课信息";
        [topView initOption];
        _navView = topView;
    }
    return _navView;
}
- (BuKeInforTopSearchView *)topView{
    if (!_topView) {
        BuKeInforTopSearchView *view = [[BuKeInforTopSearchView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 63)];
        view.backgroundColor = [UIColor whiteColor];
        _topView = view;
    }
    return _topView;
}
- (void)leftTestButtonClick{
    __weak typeof(self) weakSelf = self;
    [CJYearMonthSelectedView showDatePickerWithTitle:@"选择月份" minDateStr:@"2000-01" resultBlock:^(NSString *selectValue) {
        
        if ([selectValue isEqualToString:@"全部"]) {
            selectValue = @"";
        }
        
        //选择完成后的操作
        //NSArray *array = [selectValue componentsSeparatedByString:@"-"];//
        self->pageIndex = 1;
        weakSelf.dateTime = selectValue;
        [self->_dataListArr removeAllObjects];
        [weakSelf requestDataListArr];
    }];
}
- (void)rightTestButtonClick{
    __weak typeof(self) weakSelf = self;
    [CJClassSelectedView showDatePickerWithTitle:@"选择班级" minDateStr:@"年级一" resultBlock:^(TeacherClassListModel * _Nonnull selectValue) {
       // NSLog(@"输出选择的班级 %@",selectValue);
        self->pageIndex = 1;
        [self->_dataListArr removeAllObjects];
        weakSelf.banjiid = selectValue.id;
        [weakSelf requestDataListArr];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 144, self.view.frame.size.width, APP_HEIGHT-84) style:UITableViewStylePlain];
        tableView.delegate  =self;
        tableView.dataSource = self;
        tableView.backgroundColor = [UIColor whiteColor];
        [tableView registerClass:[BuKeInfoemationCell class] forCellReuseIdentifier:@"BuKeInfoemationCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //TOP
    return self.dataListArr.count;
//    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SYBuKeInfoTableViewCell *infoCell = [tableView dequeueReusableCellWithIdentifier:@"SYBuKeInfoTableViewCell" forIndexPath:indexPath];
    if (indexPath.row%2) {
        infoCell.backgroundColor = [UIColor whiteColor];
    } else {
        infoCell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
    }
    SYBuKeXinXiModel *bkxx = self.dataListArr[indexPath.row];
    infoCell.bukeModel = bkxx;
    infoCell.block = ^(SYBuKeXinXiModel *xinxiModel) {
        [self goBuKeDetail:indexPath];
    };
    
    return infoCell;
    
    
//    BuKeInfoemationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BuKeInfoemationCell" forIndexPath:indexPath];
//    if (!cell) {
//        cell = [[BuKeInfoemationCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BuKeInfoemationCell"];
//    }
//    cell.indexPath = indexPath;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    if (indexPath.row%2 == 0) {
//        cell.backgroundColor = [UIColor whiteColor];
//    }else{
//        cell.backgroundColor = [UIColorChange colorwithHexString:@"#F8F8F8"];
//    }
//    //TOP
////    SYBuKeXinXiModel *bkxx = self.dataListArr[indexPath.row];
////    NSString *leixing = [bkxx.isOnLine isEqualToString:@"0"]?@"线上":@"线下";
//    cell.dataArray = @[bkxx.serial,bkxx.name,bkxx.courseName,bkxx.lesson,leixing,bkxx.beginDate,@" " ];
//
////    cell.dataArray = @[@"14258",@"姓名",@"Lession One",@"1",@"线下",@"2018-12-01-10 18:00:00",@" "];
//    [cell reloadViewLayout];
//    __block BuKeInformationVC *mySelf = self;
//    cell.mainBlock = ^(NSIndexPath *index) {
//        [mySelf goBuKeDetail:index];
//    };
//    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   // NSLog(@"点击了cell");
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (!_titleView) {
        _titleView = [[BKXXCellHeaderView alloc]initWithFrame:CGRectMake(0, 0, _mainw+20, 44) andArray:@[@"学号",@"姓名",@"教材",@"课时",@"类型",@"时间",@"出勤"]];
        _titleView.backgroundColor = CONTENTBACKCOLOR;
    }
    return _titleView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    mainView.frame = CGRectMake(0, 0, self.view.frame.size.width, 30);
    return mainView;
}
#pragma mark -- 前往补课详情
- (void)goBuKeDetail:(NSIndexPath *)indexPath{
     SYBuKeXinXiModel *bkxx = self.dataListArr[indexPath.row];
    _buKeXiangQingVC = [[BuKeInformationDetailVC alloc]init];
    _buKeXiangQingVC.view.frame = CGRectMake(self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
    [_buKeXiangQingVC requestDataListTutorApplyId:bkxx.bkid isOnLine:bkxx.isOnLine];
    [self.view addSubview:_buKeXiangQingVC.view];
    [UIView animateWithDuration:0.5 animations:^{
        self->_buKeXiangQingVC.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    }];
}
- (void)changeView:(NSNotification *)info{
    BOOL type = [[info.userInfo objectForKey:@"Type"] intValue];
    self.Type = !type;
    _mainw = [PublickFunctionObject currentScreenSize:type];
   // NSLog(@"大小 %f",_mainw);
    self.tableList.frame = CGRectMake(0, 0, _mainw, self.view.frame.size.height);
    [self.tableList reloadData];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
